﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using Business;
using Entity;

namespace TaskManager.Models
{
    internal class DiskFileStore : IFileStore
    {
        private string _uploadsFolder = HostingEnvironment.MapPath("~/Uploads");

        public int SaveUploadedFile(HttpPostedFileBase fileBase)
        {
            var identifier = Guid.NewGuid();
            var filePath = identifier.ToString() + "_" + fileBase.FileName;
            fileBase.SaveAs(GetDiskLocation(filePath));
            var document = new Document
                {
                    Name = fileBase.FileName,
                    FilePath = filePath
                };
            DocumentBO.Insert(document);

            return document.Id;
        }

        private string GetDiskLocation(string fileName)
        {
            return Path.Combine(_uploadsFolder, fileName);
        }
    }
}